package tools.chapter23.FutureDemo;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

/**
 * @author Kevin
 * @date 2020/4/14 22:26
 */
public class FutureMain {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executor = Executors.newFixedThreadPool(1);
        //构造FutureTask
        FutureTask<String> future = new FutureTask<String>(new RealData("a"));
        //执行FutureTask。相当于上例中的client.
        //在这里开启线程进行RealData的call()方法
        executor.submit(future);

        System.out.println("请求完毕");
        try {
            //这里依然可以做额外的数据操作，使用sleep代替其他业务逻辑的处理
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //相当于5.5.2节中的data.getResult()方法。取得call()方法的返回值
        //如果当时call()方法没有执行完成，则依然会等待
        System.out.println("数据 = " + future.get());
        executor.shutdown();
    }
}
